import loki from "lokijs";
import LokiIndexedAdapter from "lokijs/src/loki-indexed-adapter";

export interface MockOptions {
  version: number;
}

export abstract class MockDatabaseBase {
  protected db!: Loki;
  protected options: MockOptions;

  constructor(appname: string, options: MockOptions) {
    // 连接数据库
    const _idbAdapter = new LokiIndexedAdapter(appname);
    const _this = this;

    this.options = options;

    // 仅当您希望单个集合超过50兆左右时才使用分页
    // 不确定测试数据的实际大小，开启分页功能
    const _pa = new loki.LokiPartitioningAdapter(_idbAdapter, { paging: true });

    this.db = new loki("test", {
      adapter: _pa,
      autoload: true,
      autoloadCallback: () => {
        _this.databaseInitialize();
      },
      autosave: true,
      autosaveInterval: 4000
    });
  }

  abstract databaseInitialize(): void;
}
